package com.aboo.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import org.apache.log4j.Logger;

/**
 * @author Yata Aboo
 * @Date 2012-10-8
 * @Desc 加解密工具包
 */
public class EncryptionUtil {
	
	private static Logger log = Logger.getLogger(EncryptionUtil.class);

	/**
	 * MD5加密算法
	 * @param plainText 待代码内容
	 * @return 加密后内容
	 */
	public static String encodeMD5(String plainText) {
		StringBuffer buf = new StringBuffer("");
		try {
			MessageDigest md = MessageDigest.getInstance("MD5");
			md.update(plainText.getBytes());
			byte b[] = md.digest();
			int i;
			for (int offset = 0; offset < b.length; offset++) {
				i = b[offset];
				if (i < 0)
					i += 256;
				if (i < 16)
					buf.append("0");
				buf.append(Integer.toHexString(i));
			}
		} catch (NoSuchAlgorithmException e) {
			log.error("MD5加密错误", e);
		}
		return buf.toString();
	}
}
